﻿Imports System.Collections.Generic

'© 2004 - 2008 ActiveModules, Inc. All Rights Reserved
Namespace DotNetNuke.Modules.ActiveForums
#Region "Templates"
    Public Class Templates
        Enum TemplateTypes
            All         '0
            System      '1
            ForumView   '2
            TopicView   '3
            TopicsView  '4
            TopicForm   '5
            ReplyForm   '6
            QuickReplyForm  '7
            Email       '8
            Profile     '9
            ModEmail    '10
            PostInfo    '11
            MailConnector   '12
        End Enum

    End Class
#End Region
#Region "TemplateInfo"
    Public Class TemplateInfo
#Region "Private Members"
        Private _TemplateId As Integer
        Private _PortalId As Integer
        Private _ModuleId As Integer
        Private _TemplateType As Templates.TemplateTypes
        Private _IsSystem As Boolean
        Private _Title As String
        Private _Subject As String
        Private _Template As String
        Private _TemplateHTML As String
        Private _TemplateText As String
        Private _DateCreated As Date
        Private _DateUpdated As Date
#End Region
#Region "Public Properties"
        Public Property TemplateId() As Integer
            Get
                Return _TemplateId
            End Get
            Set(ByVal value As Integer)
                _TemplateId = value
            End Set
        End Property
        Public Property PortalId() As Integer
            Get
                Return _PortalId
            End Get
            Set(ByVal value As Integer)
                _PortalId = value
            End Set
        End Property
        Public Property ModuleId() As Integer
            Get
                Return _ModuleId
            End Get
            Set(ByVal value As Integer)
                _ModuleId = value
            End Set
        End Property
        Public Property TemplateType() As Templates.TemplateTypes
            Get
                Return _TemplateType
            End Get
            Set(ByVal value As Templates.TemplateTypes)
                _TemplateType = value
            End Set
        End Property
        Public Property IsSystem() As Boolean
            Get
                Return _IsSystem
            End Get
            Set(ByVal value As Boolean)
                _IsSystem = value
            End Set
        End Property
        Public Property Subject() As String
            Get
                Return _Subject
            End Get
            Set(ByVal value As String)
                _Subject = value
            End Set
        End Property
        Public Property Title() As String
            Get
                Return _Title
            End Get
            Set(ByVal value As String)
                _Title = value
            End Set
        End Property
        Public Property Template() As String
            Get
                Return _Template
            End Get
            Set(ByVal value As String)
                _Template = value
            End Set
        End Property
        Public Property TemplateHTML() As String
            Get
                Return _TemplateHTML
            End Get
            Set(ByVal value As String)
                _TemplateHTML = value
            End Set
        End Property
        Public Property TemplateText() As String
            Get
                Return _TemplateText
            End Get
            Set(ByVal value As String)
                _TemplateText = value
            End Set
        End Property
        Public Property DateCreated() As Date
            Get
                Return _DateCreated
            End Get
            Set(ByVal value As Date)
                _DateCreated = value
            End Set
        End Property
        Public Property DateUpdated() As Date
            Get
                Return _DateUpdated
            End Get
            Set(ByVal value As Date)
                _DateUpdated = value
            End Set
        End Property
#End Region

    End Class
#End Region
#Region "Template Controller"
    Public Class TemplateController
#Region "Public Methods"
        ''<summary>
        ''Function to save template.</summary>
        ''<param name="info">TemplateInfo object</param>
        Public Function Template_Save(ByVal info As TemplateInfo) As Integer
            Return CType(DataProvider.Instance().Templates_Save(info.TemplateId, info.PortalId, info.ModuleId, info.TemplateType, info.IsSystem, info.Title, info.Subject, info.Template), Integer)
        End Function
        Public Function Template_List(ByVal PortalId As Integer, ByVal ModuleId As Integer) As List(Of TemplateInfo)
            Return GetTemplateList(PortalId, ModuleId, Templates.TemplateTypes.All)
        End Function
        Public Function Template_List(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal TemplateType As Templates.TemplateTypes) As List(Of TemplateInfo)
            Return GetTemplateList(PortalId, ModuleId, TemplateType)
        End Function

        Public Sub Template_Delete(ByVal TemplateId As Integer, ByVal PortalId As Integer, ByVal ModuleId As Integer)
            DataProvider.Instance().Templates_Delete(TemplateId, PortalId, ModuleId)
        End Sub
        Public Function Template_Get(ByVal TemplateName As String, ByVal PortalId As Integer, ByVal ModuleId As Integer) As TemplateInfo
            Dim ti As TemplateInfo = Nothing
            For Each ti In Template_List(PortalId, ModuleId)
                If TemplateName.ToUpperInvariant = ti.Title.ToUpperInvariant Then
                    ti.TemplateHTML = GetHTML(ti.Template)
                    ti.TemplateText = GetText(ti.Template)
                    Return ti
                    Exit For
                End If
            Next
            Return ti
        End Function
        Public Function Template_Get(ByVal TemplateId As Integer) As TemplateInfo
            Return Template_Get(TemplateId, -1, -1)
        End Function
        Public Function Template_Get(ByVal TemplateId As Integer, ByVal PortalId As Integer, ByVal ModuleId As Integer) As TemplateInfo
            Dim info As New TemplateInfo
            Dim dr As IDataReader = DataProvider.Instance().Templates_Get(TemplateId, PortalId, ModuleId)
            While dr.Read
                With info
                    Try
                        .TemplateId = CInt(dr("TemplateId"))
                        .PortalId = CInt(dr("PortalId"))
                        .ModuleId = CInt(dr("ModuleId"))
                        .TemplateType = CType(CInt(dr("TemplateType")), Templates.TemplateTypes)
                        .IsSystem = CBool(dr("IsSystem"))
                        .Title = CStr(dr("Title"))
                        .Subject = CStr(dr("Subject"))
                        .Template = CStr(dr("Template"))
                        .TemplateHTML = GetHTML(CStr(dr("Template")))
                        .TemplateText = GetText(CStr(dr("Template")))
                        If dr.IsDBNull(8) Then
                            .DateCreated = Utilities.NullDate
                        Else
                            .DateCreated = CDate(dr("DateCreated"))
                        End If
                        If dr.IsDBNull(9) Then
                            .DateUpdated = Utilities.NullDate
                        Else
                            .DateUpdated = CDate(dr("DateUpdated"))
                        End If

                    Catch ex As Exception
                        Return Nothing
                    End Try

                End With

            End While
            Return info
        End Function
#End Region
#Region "Private Methods"
        Private Function GetHTML(ByVal Template As String) As String
            Try
                If Template.Contains("<html>") Then
                    Dim sHTML As String = String.Empty
                    Dim xDoc As New System.Xml.XmlDocument
                    xDoc.LoadXml(Template)
                    Dim xNode As System.Xml.XmlNode
                    Dim xRoot As System.Xml.XmlNode = xDoc.DocumentElement
                    xNode = xRoot.SelectSingleNode("/template/html")
                    sHTML = xNode.InnerText
                    Return sHTML
                Else
                    Return Template
                End If
            Catch ex As Exception
                Return Template
            End Try

        End Function
        Private Function GetText(ByVal Template As String) As String
            Try
                If Template.Contains("<plaintext>") Then
                    Dim sText As String = String.Empty
                    Dim xDoc As New System.Xml.XmlDocument
                    xDoc.LoadXml(Template)
                    Dim xNode As System.Xml.XmlNode
                    Dim xRoot As System.Xml.XmlNode = xDoc.DocumentElement
                    xNode = xRoot.SelectSingleNode("/template/plaintext")
                    sText = xNode.InnerText
                    Return sText
                Else
                    Return Template
                End If

            Catch ex As Exception
                Return Template
            End Try

        End Function
        Private Function GetTemplateList(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal TemplateType As Templates.TemplateTypes) As List(Of TemplateInfo)
            Try
                Dim tl As New List(Of TemplateInfo)
                Dim dr As IDataReader
                If TemplateType = Templates.TemplateTypes.All Then
                    dr = DataProvider.Instance().Templates_List(PortalId, ModuleId, -1)
                Else
                    dr = DataProvider.Instance().Templates_List(PortalId, ModuleId, TemplateType)
                End If
                dr.Read()
                dr.NextResult()
                Dim ti As TemplateInfo
                While dr.Read
                    ti = New TemplateInfo
                    With ti
                        If dr.IsDBNull(8) Then
                            .DateCreated = Utilities.NullDate
                        Else
                            .DateCreated = CDate(dr("DateCreated"))
                        End If
                        If dr.IsDBNull(9) Then
                            .DateUpdated = Utilities.NullDate
                        Else
                            .DateUpdated = CDate(dr("DateUpdated"))
                        End If
                        .IsSystem = CBool(dr("IsSystem"))
                        .ModuleId = CInt(dr("ModuleID"))
                        .PortalId = CInt(dr("PortalId"))
                        .Subject = CStr(dr("Subject"))
                        .Template = CStr(dr("Template"))
                        .TemplateHTML = GetHTML(.Template)
                        .TemplateId = CInt(dr("TemplateId"))
                        .TemplateText = GetText(.Template)
                        .TemplateType = CType(dr("TemplateType"), Templates.TemplateTypes)
                        .Title = CStr(dr("Title"))
                    End With
                    tl.Add(ti)
                End While
                dr.Close()
                Return tl
            Catch ex As Exception
                Return Nothing
            End Try
        End Function
#End Region
    End Class
#End Region
End Namespace
